package leetcode_500;

/**
 *@author 周杨
 *LicenseKeyFormatting_482 format一个字符串
 *describe:反转 用哨兵记录count AC 83%
 *2018年8月31日 下午6:47:06
 */
public class LicenseKeyFormatting_482 {
	public static void main(String[] args) {
		LicenseKeyFormatting_482 test=new LicenseKeyFormatting_482();
		System.out.println(test.licenseKeyFormatting("5F3Z-2e-9-w", 4));
	}
	
	public String licenseKeyFormatting(String S, int K) {
        StringBuilder sb=new StringBuilder();
        int count=0;//每四个生成一个-
        for(int i=S.length()-1;i>=0;--i) {
        	char c=S.charAt(i);
        	if(c<='z'&&c>='a') {//转大写
        		c-=32;
        		if(count==K) {
        			sb.append('-').append(c);
        			count=1;
        		}
        		else {
        			sb.append(c);
        			++count;
        		}
        	}
        	else if(c<='Z'&&c>='A'||c<='9'&&c>='0') {
        		if(count==K) {
        			sb.append('-').append(c);
        			count=1;
        		}
        		else {
        			sb.append(c);
        			++count;
        		}
        	}
        	
        }
        return sb.reverse().toString();
    }
}
